package com.bst;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

// 声明为持久化组件
@State(
        name = "BaiduTranslateSettings",
        storages = @Storage("baidu-translate-settings.xml")
)
public class BaiduTranslateSettings implements PersistentStateComponent<BaiduTranslateSettings> {
    public String appId = "";
    public String appKey = "";
    public String defaultFrom = "auto";
    public String defaultTo = "zh";

    // 新的单例获取方式（替代ServiceManager）
    public static BaiduTranslateSettings getInstance() {
        // 从当前应用中获取服务实例
        return com.intellij.openapi.components.ServiceManager.getService(BaiduTranslateSettings.class);
        // 注意：如果是项目级别的配置（非全局），使用下面的方式：
        // return project.getService(BaiduTranslateSettings.class);
    }

    // 添加配置检查方法：判断appId和appKey是否已填写
    public boolean isConfigured() {
        // 简单判断：appId和appKey都不为空即视为已配置
        return appId != null && !appId.trim().isEmpty()
                && appKey != null && !appKey.trim().isEmpty();
    }

    @Nullable
    @Override
    public BaiduTranslateSettings getState() {
        return this;
    }

    @Override
    public void loadState(@NotNull BaiduTranslateSettings state) {
        XmlSerializerUtil.copyBean(state, this);
    }
}


